# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : mergeFx.py
# Date      : 2019/11/26
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------
"""
import sys
path = r"P:\AIRBNB\tools\script"
if path not in sys.path:
    sys.path.append(path)
import IkFk
IkFk.run()
"""
# select ikfk switch control

import maya.cmds as mc

def emptyGroup(obj, emptyStrs=["Zero"]):
    hi = mc.listRelatives(obj, p=True)
    nameList = list()
    pat = obj
    for empty in emptyStrs:
        grp = mc.group(em=True, name='_'.join([obj, empty]), parent=pat)
        pat = grp
        nameList.append(grp)
    if hi: mc.parent(nameList[0], hi[0])
    else: mc.parent(nameList[0], w=True)
    mc.parent(obj, grp)
    return nameList
def toDef(inobj):
    mc.setAttr(inobj + ".t", 0, 0, 0)
    mc.setAttr(inobj + ".r", 0, 0, 0)
    mc.setAttr(inobj + ".s", 1, 1, 1)
    return
def to(sc, tgt_ctrl, sobj, mode=0):
    tgt_trans = mc.createNode("transform", name=tgt_ctrl + "_t")
    mc.parent(tgt_trans, tgt_ctrl)
    toDef(tgt_trans)
    tgrp = emptyGroup(tgt_trans)
    mc.parent(tgrp, w=True)
    dup1 = mc.createNode("transform", name=sobj + "_t")
    mc.parent(dup1, tgt_ctrl)
    toDef(dup1)
    ikfk = mc.getAttr(sc + ".ikFkSwitch")
    if ikfk: mc.setAttr(sc + ".ikFkSwitch", 0)
    else: mc.setAttr(sc + ".ikFkSwitch", 1)
    mc.parent(dup1, sobj)
    mc.setAttr(sc + ".ikFkSwitch", ikfk)
    dgrp = emptyGroup(dup1)
    mc.parent(dgrp, w=True)
    return dup1, tgt_trans
def run():
    sel = mc.ls(sl=1)
    for sl in sel:
        ns = sl.split(":")
        nn = ns[1].split("_")
        ikfk = mc.getAttr(sl + ".ikFkSwitch")
        lname = "{}:{}_{}".format(ns[0], nn[0], nn[1])
        if ikfk:  # fk >>> ik
            if nn[1] == "Arm":
                handle_ctrl = lname + "_Ik_ctrl"
                drjot = lname + "_Dr_Wrist"
                hc, tc = to(sl, handle_ctrl, drjot)
                pctrl = lname + "_Polar_ctrl"
                drjot = lname + "_Dr_ForeArm"
                hc1, tc1 = to(sl, pctrl, drjot)
            else:
                handle_ctrl = lname + "_Ik_ctrl"
                drjot = lname + "_Dr_Ankle"
                hc, tc = to(sl, handle_ctrl, drjot)
                pctrl = lname + "_Polar_ctrl"
                drjot = lname + "_Dr_Shank"
                hc1, tc1 = to(sl, pctrl, drjot)
            hpc = mc.parentConstraint(tc, handle_ctrl, mo=False)
            mc.delete(mc.parentConstraint(hc, tc, mo=False))
            mc.delete(hpc)
            hpc = mc.parentConstraint(tc1, pctrl, mo=False)
            mc.delete(mc.parentConstraint(hc1, tc1, mo=False))
            mc.delete(hpc)
            mc.setAttr(sl + ".ikFkSwitch", 0)
            for _ht in [hc, tc, hc1, tc1]: mc.delete(_ht + "_Zero")
        else:
            arm = [["Arm_ctrl", "Arm"], ["ForeArm_ctrl", "ForeArm"], ["Wrist_ctrl", "Hand"]]
            leg = [["Leg_ctrl", "Leg"], ["Shank_ctrl", "Shank"], ["Ankle_ctrl", "Ankle"]]
            ht = []
            hp = []
            if nn[1] == "Arm": ilist = arm
            else: ilist = leg
            for ctrl, drjot in ilist:
                hc, tc = to(sl, lname + "_Fk_" + ctrl, lname + "_Dr_" + drjot, mode=1)
                ht.append([hc, tc])
                hpc = mc.parentConstraint(tc, lname + "_Fk_" + ctrl, mo=False)[0]
                hp.append(hpc)
            for h, t in ht: mc.delete(mc.parentConstraint(h, t, mo=False))
            mc.setAttr(sl + ".ikFkSwitch", 1)
            mc.delete(hp)
            for h, t in ht: mc.delete(h + "_Zero", t + "_Zero")

if __name__ == '__main__':
    pass